/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import { hvigorCore } from '@ohos/hvigor';

import { SdkInfo } from '../sdk/sdk-info.js';

function updateToolChainsVersionWhenChanged(sdkInfo: SdkInfo) {
  const toolChainsVersion = 'toolChainsVersion';
  const curToolChainsVersion = sdkInfo.getToolchainsComponentVersion();
  if (hvigorCore.getMetaProperty(toolChainsVersion) !== curToolChainsVersion) {
    hvigorCore.updateMetaProperty(toolChainsVersion, curToolChainsVersion);
    return true;
  }
  return false;
}

export function hvigorOrToolChainsChanged(sdkInfo: SdkInfo) {
  // 目前无法获取sdk整体的版本号，暂时使用toolchains的版本号代替
  // 确保toolChainsVersion写入meta.json
  const toolChainsVersionChanged = updateToolChainsVersionWhenChanged(sdkInfo);
  return hvigorCore.versionChanged() || toolChainsVersionChanged;
}

export function updateMetaCompileSdkVersion(compileSdkVersion: string | number) {
  hvigorCore.updateMetaProperty('compileSdkVersion', compileSdkVersion as string);
}
